﻿#region Copyright
//————————————————————————————————————————//
//	创建人员(Creator)    ：Huangyd
//	创建日期(Date)       ：2012-07-14
//  联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
//  描    述(Description)：
//
//	Copyright (C) 2012 ajayumi 版权所有
//————————————————————————————————————————//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Security.Permissions;

namespace  ajayumi.develop.CSharp.Controls.Forms
{
    public abstract class RenderBase
    {
        #region variable

        private EventHandlerList m_Events;

        private static readonly object CaptionRenderEvent = new object();
        private static readonly object BorderRenderEvent = new object();
        private static readonly object BackgroundRenderEvent = new object();
        private static readonly object ControlBoxRenderEvent = new object();

        #endregion

        #region Constructors

        protected RenderBase()
        { }

        #endregion

        #region Properties

        protected EventHandlerList Events
        {
            get
            {
                if (m_Events == null)
                {
                    m_Events = new EventHandlerList();
                }
                return m_Events;
            }
        }

        #endregion

        #region Events

        public event CaptionRenderEventHandler CaptionRender
        {
            add { AddHandler(CaptionRenderEvent, value); }
            remove { RemoveHandler(CaptionRenderEvent, value); }
        }

        public event BorderRenderEventHandler BorderRender
        {
            add { AddHandler(BorderRenderEvent, value); }
            remove { RemoveHandler(BorderRenderEvent, value); }
        }

        public event BackgroundRenderEventHandler BackgroundRender
        {
            add { AddHandler(BackgroundRenderEvent, value); }
            remove { RemoveHandler(BackgroundRenderEvent, value); }
        }

        public event ControlBoxRenderEventHandler ControlBoxRender
        {
            add { AddHandler(ControlBoxRenderEvent, value); }
            remove { RemoveHandler(ControlBoxRenderEvent, value); }
        }

        #endregion

        #region Public Methods

        public abstract Region CreateRegion(FormBase form);

        public abstract void InitForm(FormBase form);

        public void DrawCaption(CaptionRenderEventArgs e)
        {
            OnCaptionRender(e);
            CaptionRenderEventHandler handle = Events[CaptionRenderEvent] as CaptionRenderEventHandler;
            if (handle != null)
            {
                handle(this, e);
            }
        }

        public void DrawBorder(BorderRenderEventArgs e)
        {
            OnBorderRender(e);
            BorderRenderEventHandler handle = Events[BorderRenderEvent] as BorderRenderEventHandler;
            if (handle != null)
            {
                handle(this, e);
            }
        }


        public void DrawBackground(BackgroundRenderEventArgs e)
        {
            OnBackgroundRender(e);
            BackgroundRenderEventHandler handle = Events[BackgroundRenderEvent] as BackgroundRenderEventHandler;
            if (handle != null)
            {
                handle(this, e);
            }
        }

        public void DrawControlBox(ControlBoxRenderEventArgs e)
        {
            OnControlBoxRender(e);
            ControlBoxRenderEventHandler handle = Events[ControlBoxRenderEvent] as ControlBoxRenderEventHandler;
            if (handle != null)
            {
                handle(this, e);
            }
        }

        #endregion

        #region Protected Render Methods

        protected abstract void OnCaptionRender(CaptionRenderEventArgs e);

        protected abstract void OnBorderRender(BorderRenderEventArgs e);

        protected abstract void OnBackgroundRender(BackgroundRenderEventArgs e);

        protected abstract void OnControlBoxRender(ControlBoxRenderEventArgs e);

        #endregion

        #region Protected Methods

        [UIPermission(SecurityAction.Demand, Window = UIPermissionWindow.AllWindows)]
        protected void AddHandler(object key, Delegate value)
        {
            Events.AddHandler(key, value);
        }

        [UIPermission(SecurityAction.Demand, Window = UIPermissionWindow.AllWindows)]
        protected void RemoveHandler(object key, Delegate value)
        {
            Events.RemoveHandler(key, value);
        }

        #endregion
    }
}
